package cn.neusoft.edu.his.demo.controller;

import cn.neusoft.edu.his.demo.utils.JWTUtils;
import io.swagger.annotations.Api;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.neusoft.edu.his.demo.common.Result;
import cn.neusoft.edu.his.demo.mapper.UserMapper;
import cn.neusoft.edu.his.demo.mapper.UserRoleMapper;
import cn.neusoft.edu.his.demo.po.User;
import cn.neusoft.edu.his.demo.po.UserRole;

@CrossOrigin
@Api("UserController相关的api")
@RestController
public class UserController {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserRoleMapper userRoleMapper;
	
	@RequestMapping("/getRoleList")
	public Result getRoleList(){
		List<UserRole> list=userRoleMapper.selectList(null);
		return Result.ok(list);
	}
	
	@RequestMapping("/registerUser")
	public Result registerUser(User user){
		User u=userMapper.checkUserName(user);
		if(u!=null)
			return Result.fail("该用户名或手机号已经被占用");
		else{			
			int i=userMapper.insert(user);
			if(i>0)
				return Result.ok("注册成功");
			else
				return Result.fail("注册失败");
		}			
	}
	
	@RequestMapping("/registerUser2")
	public int registerUser2(User user){
		User u=userMapper.checkUserName(user);
		if(u!=null)
			return -1;//该用户名或手机号已经被占用
		else			
			return userMapper.insert(user);			
	}

	@RequestMapping("/updateUser")
	public int updateUser(User user){
		 return userMapper.updateUser(user);
		
		}

	@RequestMapping("/updatePassword")
	public int updatePassword(User user){
		return userMapper.updatePassword(user);
	}

	
	@RequestMapping("/checkLogin")
	public Result checkLogin(User user){
		User u=userMapper.checkLogin(user);
		HashMap<String, Object> map = new HashMap<>();
		if(u!=null){//找到匹配的用户
			Map<String, String> payload = new HashMap<>();
			u.setLoginCount(u.getLoginCount()+1);
			u.setLastLogin(getNow());
			userMapper.updateById(u);
			//生成JWT令牌
			//payload.put("username",u.getUserName());
			String token = JWTUtils.getToken(payload);
			map.put("token",token);
			return Result.ok("登录成功",u,token);
		}			
		else//没有用户
			return Result.fail("登录失败");
	}
	
	
	@RequestMapping("/checkLogin2")
	public Object checkLogin2(User user){
		User u=userMapper.checkLogin(user);
		if(u!=null){//找到匹配的用户
			u.setLoginCount(u.getLoginCount()+1);
			u.setLastLogin(getNow());
			userMapper.updateById(u);
			System.out.println("1231");
			return u;			
		}			
		else//没有用户
			System.out.println("123551");
			return 0;
	}



	@RequestMapping("/getUsers")
	@ResponseBody
	public Result getUsers(){
		List<User> u=userMapper.getUsers();
		return Result.ok(u);
	}

	
   public Date getNow()
   {
	   SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	   Date date = new Date(System.currentTimeMillis());
	   return date;
   }
}
